import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    carts : []
  }

  methods = {
    onChange(e) {
      let id = e.currentTarget.dataset.id
      let count = e.detail
      
      this.$parent.updateCarts(id, count)
    },
    checkChange(e) {
      let id = e.currentTarget.dataset.id
      let check = e.detail

      this.$parent.checkCarts(id, check)
    },
    removeGoods(id) {
      this.$parent.removeCarts(id)
    },
    allCheckChange(e) {
      let check = e.detail

      this.$parent.allCheckChange(check)
    },
    onClickButton() {
      if(this.allPrice <= 0) {
        return 
      }

      wepy.navigateTo({
        url: '/pages/order'
      })
    }
  }

  onLoad() {
    this.carts = this.$parent.globalData.carts

    this.$parent.setBadge()
  }

  computed = {
    allPrice() {
      let total = 0

      this.carts.forEach(item => {
        if(item.isCheck) {
          total += item.price * item.count
        }
      })

      return total * 100
    },

    allCheckBtn() {
      return this.carts.every(item => item.isCheck === true)
    }
  }
}